package cn.topevery.garden.dal.dao.impl;

import cn.topevery.garden.dal.dao.GiveAnAlarmDao;
import cn.topevery.garden.entity.po.GiveAnAlarm;
import cn.topevery.garden.entity.query.GiveAnAlarmQuery;
import cn.topevery.garden.entity.vo.GiveAnAlarmVO;
import cn.topevery.garden.framework.base.BaseDaoImpl;
import hw.topevery.framework.db.CommandType;
import hw.topevery.framework.db.entity.DataPager;
import hw.topevery.framework.db.entity.SqlQueryMate;
import hw.topevery.framework.db.enums.ScriptConditionEnum;
import hw.topevery.framework.entity.DbCsp;
import hw.topevery.framework.entity.KeyValue;
import hw.topevery.framework.entity.Value;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * 人车报警记录表
 *
 * @author code-generate
 * @date 2022-01-18
 */
@Repository
public class GiveAnAlarmDaoImpl extends BaseDaoImpl<GiveAnAlarm> implements GiveAnAlarmDao {
    @Override
    public <P extends DbCsp> List<GiveAnAlarm> getSearch(P para) {
        return null;
    }

    

    @Override
    public DataPager<GiveAnAlarmVO> listPage(GiveAnAlarmQuery query) {
        String table = "t_give_an_alarm tgaa";

        String columns = "tgaa.*,(select c_park_name from t_person p where p.c_id = tgaa.c_obj_id_int and tgaa.c_obj_type=0 LIMIT 1) c_park_name";

        int pageIndex = query.pageIndex - 1;
        int pageSize = query.pageSize;

        SqlQueryMate sqlQueryMate = new SqlQueryMate(table).setColumns(columns).setOrderBy("tgaa.c_begin_time desc");
        sqlQueryMate
                .where(true, "tgaa.c_db_status", ScriptConditionEnum.Eq, 0)
                .where(StringUtils.isNotBlank(query.userId),"tgaa.c_obj_id_int", ScriptConditionEnum.Eq, query.userId)
                .where(StringUtils.isNotBlank(query.userId),"tgaa.c_obj_type", ScriptConditionEnum.Eq, 0)
                .where(StringUtils.isNotBlank(query.getCode()),"tgaa.c_status", ScriptConditionEnum.Eq, query.getCode())
                .where(StringUtils.isNotBlank(query.getTypeName()),"tgaa.c_type", ScriptConditionEnum.Eq, query.getTypeName())
                .where(StringUtils.isNotBlank(query.beginDate),"tgaa.c_begin_time", ScriptConditionEnum.GreaterThanEq, query.beginDate)
                .where(StringUtils.isNotBlank(query.endDate), "tgaa.c_begin_time", ScriptConditionEnum.LessThanEq, query.endDate)
                .where(StringUtils.isNotBlank(query.getName()), "tgaa.c_obj_name", ScriptConditionEnum.Like, query.getName())
                .where(StringUtils.isNotBlank(query.getExtentName()), "tgaa.c_extent_name", ScriptConditionEnum.Eq, query.getExtentName());

        return getDataPager(sqlQueryMate, pageSize, pageIndex, GiveAnAlarmVO.class);
    }

    @Override
    public GiveAnAlarm getEntityByColumn(String key,String value){
        KeyValue<String> map=new KeyValue<>();
        map.key=key;
        map.value=value;
        return getEntityByColumnEqValue(map);
    }

    @Override
    public Boolean synAlerm() {
        Value<Boolean> res = new Value<>();
        String sql = "call p_give_an_alarm";
        run(sql, CommandType.Text, dbCmd -> {
            res.data = dbCmd.executeNonQuery();
        });
        return res.data;
    }
}
